//
//  TestingViewController.m
//  Light
//
//  Created by 骁唐 on 15/9/7.
//  Copyright (c) 2015年 骁唐. All rights reserved.
//

#import "TestingViewController.h"
#import "AFNetworking.h"
#import "NetworkTool.h"
#import "UserConfig.h"

@interface TestingViewController ()<UITextFieldDelegate>
//验证码
@property (weak, nonatomic) IBOutlet UITextField *verificationText;
//倒计时btn
@property (weak, nonatomic) IBOutlet UIButton *timeBtn;
//手机号lebal
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
//背景ImageView
@property (weak, nonatomic) IBOutlet UIImageView *backGround;
//注册按钮
@property (weak, nonatomic) IBOutlet UIButton * buttonNext;
@end

@implementation TestingViewController
{
    NSTimer *timerr;
    int timerNumber;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.phoneLabel.text = [NSString stringWithFormat:@"“%@”",self.phoneText];
    
    self.verificationText.keyboardType = UIKeyboardTypeNumberPad;
    self.verificationText.delegate = self;
    timerNumber = 60;

    timerr = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
    [timerr setFireDate:[NSDate distantFuture]];//暂停
    [self.timeBtn  sendActionsForControlEvents:UIControlEventTouchUpInside];//代码点击
}

#pragma mark -
#pragma mark 点击空白处，隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.verificationText resignFirstResponder];
}

#pragma mark -
#pragma mark 点击获取验证码按钮
- (IBAction)timeBtnClick
{
    [timerr setFireDate:[NSDate distantPast]];//开启定时器
    NSDictionary *dic= @{@"cellPhone":self.phoneText};
    AFHTTPRequestOperationManager *httpManager = [AFHTTPRequestOperationManager manager];
    httpManager.requestSerializer = [[AFHTTPRequestSerializer alloc]init];
    httpManager.responseSerializer = [[AFHTTPResponseSerializer alloc]init];
    //地址
    [httpManager POST:HOME_YONGHUYZ parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *retDic = [NetworkTool toNSDictionary:responseObject];
        if ([retDic[@"flag"] intValue] == 1)
        {
            NSLog(@"发送成功");
        }
        else if([retDic[@"flag"] intValue] == -2)
        {
            TJJALERT_VIEW(@"验证码不能重复发送");
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        TJJALERT_VIEW(ERROR_NETWORKINFO);
    }];
}

#pragma mark -
#pragma mark 倒计时，修改button标题
- (void)scrollTimer
{
    if (timerNumber >0)
    {
        [self.timeBtn setUserInteractionEnabled:NO];//设置button的可点击性
        timerNumber--;
        [self.timeBtn setTitle:[NSString stringWithFormat:@"%ds",timerNumber] forState:UIControlStateNormal];
    }
    else
    {
        [timerr setFireDate:[NSDate distantFuture]];//暂停定时器
        [self.timeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.timeBtn setUserInteractionEnabled:YES];//设置button的可点击性
    }
}

#pragma mark -
#pragma mark 导航返回方法
- (IBAction)backClick {
    [timerr setFireDate:[NSDate distantFuture]];
    [self.timeBtn setUserInteractionEnabled:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 点击注册，注册成功之后，再跳转下一个界面
- (IBAction)nextClick {
    NSString * stringVer = self.verificationText.text;
    if (stringVer.length == 4)
    {//验证码长度正好是4
        [self.buttonNext setUserInteractionEnabled:NO];//可点击性NO
        NSDictionary *dic= @{@"cellPhone":self.phoneText,@"verification":self.verificationText.text,@"password":self.password};
        AFHTTPRequestOperationManager *httpManager = [AFHTTPRequestOperationManager manager];
        httpManager.requestSerializer = [[AFHTTPRequestSerializer alloc]init];
        httpManager.responseSerializer = [[AFHTTPResponseSerializer alloc] init];
        //地址
        [httpManager POST:URL_REGIST parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [self.buttonNext setUserInteractionEnabled:YES];//可点击性YES
            NSDictionary *retDic = [NetworkTool toNSDictionary:responseObject];
            if ([retDic[@"a"] intValue] == 1)
            {
                [timerr setFireDate:[NSDate distantFuture]];//暂停定时器
                [self.timeBtn setUserInteractionEnabled:YES];//设置验证码按钮的点击性
                UserConfig *userconfig = [UserConfig sharingUserConfig];
                [userconfig setToken:retDic[@"b"]];
                [self performSegueWithIdentifier:@"testingToName" sender:nil];//跳转到填写昵称界面
            }
            else if([retDic[@"a"] intValue] == -1)
            {
                TJJALERT_VIEW(@"验证码错误");
            }
        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self.buttonNext setUserInteractionEnabled:YES];//可点击性YES
            TJJALERT_VIEW(ERROR_NETWORKINFO);
        }];
    }
    else
    {
        TJJALERT_VIEW(@"验证码输入错误");
    }
}

#pragma mark -
#pragma mark viewWillAppear 设置背景
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //强制竖屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:
                                        UIInterfaceOrientationPortrait]  forKey:@"orientation"];
    long long int currentTimeInt = [TJJDefines getTimeIntSince1970Current];
    if (currentTimeInt>=16200&& currentTimeInt <=34200) {
        self.backGround.image = [UIImage imageNamed:@"backgroundOne"];
    }else if (currentTimeInt>34200 && currentTimeInt<=55800)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundTwo"];
    }else if (currentTimeInt>55800 && currentTimeInt<=70200)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundThree"];
    }else if(currentTimeInt >70200 || currentTimeInt<16200)
    {
        self.backGround.image = [UIImage imageNamed:@"background_night"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
